//
//  MyMeetingViewModel.swift
//  CleanArchitecture
//
//  Created by siri on 2024/11/6.
//  Copyright © 2024 Tuan Truong. All rights reserved.
//



import Combine
import UIKit
import Factory
//, ShowProductDetail
class MyMeetingViewModel: GetMyMeeting{ // swiftlint:disable:this final_class
    unowned let navigationController: UINavigationController
    
    @Injected(\.myMeetingGateway)
    var myMeetingGateway: MyMeetingGatewayProtocol
    
    init(navigationController:
         UINavigationController) {
        self.navigationController = navigationController
    }
    
    // 跳转
//    func vm_showLoginDetail() {
//        showLogin()
//    }
    
//    func vm_getProducts() -> AnyPublisher<[Product], Error> {
//        getProducts()
//    }
//    func vm_getMyMeeting() -> AnyPublisher<[MyMeeting], Error> {
//        getMyMeeting()
//    }
    deinit {
        print("MyMeetingViewModel deinit")
    }
}

// MARK: - ViewModel
extension MyMeetingViewModel: ObservableObject, ViewModel {
    struct Input {
        let loadTrigger: AnyPublisher<Void, Never>
        let reloadTrigger: AnyPublisher<Void, Never>
        let selectTrigger: AnyPublisher<Void, Never>
        let clickDateTrigger: AnyPublisher<Void, Never>
    }
    
    final class Output: ObservableObject {
        @Published var result = Res<MyMonthMeetingsOutVO>()
        @Published var appointMonthMeetingsInfoList = [MyMonthMeetingInfo]()
//        @Published var isLoading = false
//        @Published var isReloading = false
//        @Published var alert = AlertMessage()
    }
    
    func transform(_ input: Input, cancelBag: CancelBag) -> Output {
//        let config = ListFetchConfig(initialLoadTrigger: input.loadTrigger,
//                                     reloadTrigger: input.reloadTrigger,
//                                     fetchItems: self.vm_getMyMeeting)
//        
//        let (meetings, error, isLoading, isReloading) = fetchList(config: config).destructured
        
        let output = Output()
        
//        meetings
//            .map { $0 }
//            .assign(to: \.meetings, on: output)
//            .store(in: cancelBag)
//        
//        error
//            .receive(on: RunLoop.main)
//            .map { AlertMessage(error: $0) }
//            .assign(to: \.alert, on: output)
//            .store(in: cancelBag)
//        
//        isLoading
//            .assign(to: \.isLoading, on: output)
//            .store(in: cancelBag)
//        
//        isReloading
//            .assign(to: \.isReloading, on: output)
//            .store(in: cancelBag)
          input.loadTrigger.sink{
            if (SettingsManager.shared.getToken().isEmpty) {
                // 跳转到登录页
                return
            }
            // 传入当前年月日
            let response =  self.getMyMonthMeeting(appointDate: Date().getDate())
            response.sink{ completion in
                switch completion {
                case .finished:
                    // 请求成功完成，通常不需要额外处理
                    break
                case .failure(let error):
                    // 失败的请求处理
                    print(error)
                    
                }
            } receiveValue: { value in
                if (value.data != nil) {
                    // 获取当月会议信息列表
                    output.appointMonthMeetingsInfoList =  value.data?.findMyMonthMeetingsInfoList ?? []
                }
            }.store(in: cancelBag)
        }.store(in: cancelBag)
        
        // 跳转
        input.selectTrigger
            .sink(receiveValue: { indexPath in
                
            })
            .store(in: cancelBag)
        
        return output
    }
}

